<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">添加</el-button>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">批量删除</el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.searchList" clearable placeholder="请输入用户名或电话号码" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText" @selection-change="setSelectRows">
      <el-table-column show-overflow-tooltip type="selection" />
      <el-table-column label="id" prop="id" show-overflow-tooltip />
      <el-table-column label="房產名称" prop="name" show-overflow-tooltip />
      <!-- <el-table-column label="房产所属人ID" prop="ownerId" show-overflow-tooltip /> -->
      <el-table-column label="房产所属人" prop="ownerName" show-overflow-tooltip />
      <el-table-column label="省份" prop="province" show-overflow-tooltip />
      <el-table-column label="城市" prop="city" show-overflow-tooltip />
      <el-table-column label="地区" prop="area" show-overflow-tooltip />
      <el-table-column label="街道" prop="street" show-overflow-tooltip />
      <el-table-column label="详细地址" prop="detailedAddress" show-overflow-tooltip />
      <el-table-column label="联系电话" prop="ownerPhone" show-overflow-tooltip />
      <!-- <el-table-column label="邮箱" prop="email" show-overflow-tooltip /> -->

      <el-table-column label="修改时间" prop="updateTime" show-overflow-tooltip />
      <el-table-column label="操作" show-overflow-tooltip width="200">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
import { doDelete, getList } from '@/api/house/property'
import Edit from './components/PropertyEdit'

export default {
  name: 'property',
  components: { Edit },
  data() {
    return {
      //list展示的数据
      list: null,
      listLoading: true,
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      selectRows: '',
      elementLoadingText: '正在加载...',
      queryForm: {
        pageNo: 1,
        pageSize: 10,
        searchList: '',
      },
      timeOutID: null,
      // BUbianList存储原始所有数据，不改变
      BUbianList: null,
    }
  },
  created() {
    console.log('222222', 3333)
    this.fetchData()
    this.showList()
  },

  beforeDestroy() {
    clearTimeout(this.timeOutID)
  },
  methods: {
    showList() {
      console.log('展示数据')
      const data = null
    },
    setSelectRows(val) {
      this.selectRows = val
    },
    handleEdit(row) {
      if (row.id) {
        console.log('添加')
        this.$refs['edit'].showEdit(row)
      } else {
        this.$refs['edit'].showEdit()
      }
    },
    handleDelete(row) {
      if (row.id) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          const { msg } = await doDelete({ ids: [row.id] })
          this.$baseMessage(msg, 'success')
          this.fetchData()
        })
      } else {
        if (this.selectRows.length > 0) {
          const ids = this.selectRows.map((item) => item.id).join()
          this.$baseConfirm('你确定要删除选中项吗', null, async () => {
            const { msg } = await doDelete({ ids:[ids] })
            this.$baseMessage(msg, 'success')
            this.fetchData()
          })
        } else {
          this.$baseMessage('未选中任何行', 'error')
          return false
        }
      }
    },
    handleSizeChange(val) {
      this.queryForm.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.queryForm.pageNo = val
      this.fetchData()
    },
    // 搜索框逻辑
    queryData() {
      // this.list = this.BUbianList
      // let inList = this.list
      let inList = this.BUbianList
      console.log('inList', inList)
      // console.log('展示数据list', this.list[0])
      console.log('输入搜索的数据', this.queryForm.searchList)
      // this.queryForm.pageNo = 1
      // this.fetchData()

      // const Fuse = require('fuse.js')
      const fuseOptions = {
        // isCaseSensitive: false,
        includeScore: true,
        // shouldSort: true,
        includeMatches: true,
        // findAllMatches: false,
        // minMatchCharLength: 1,
        // location: 0,
        // threshold: 0.6,
        // distance: 100,
        // useExtendedSearch: false,
        // ignoreLocation: false,
        // ignoreFieldNorm: false,
        // fieldNormWeight: 1,

        // 搜索关键字
        keys: ['ownerName', 'ownerPhone'],
      }

      // const fuse = new Fuse(this.list, fuseOptions)
      // Fuse（搜索数据源，搜索条件）
      const fuse = new Fuse(inList, fuseOptions)

      // 搜索后数据
      console.log('搜索数据结果', fuse.search(this.queryForm.searchList))
      const array = fuse.search(this.queryForm.searchList)
      // 判断：搜索数组不为空，展示搜索数组，内容为空，则展示所有数据；
      if (array.length > 0) {
        // 创建一个新数组，用来放搜索结果
        const newArray = []
        for (let i = 0; i < array.length; i++) {
          console.log('这是', array[i].item)
          // 创建一个新的数组，并将第一个对象添加进去
          newArray.push(array[i].item)
        }

        // 将搜索结果赋值给list,展示数据
        this.list = newArray
        // this.list = fuse.search(this.queryForm.searchList)
      } else {
        // alert('请输入搜索内容')
        // 1、创建变量inList，存在原始BUbianList 2、没有搜索内容时，list展示原始数inList
        this.list = inList
      }
    },

    async fetchData() {
      this.listLoading = true
      const { data, totalCount } = await getList({ ownerId: 69, ...this.queryForm })
      this.list = data.list
      this.BUbianList = data.list
      // if (this.queryForm.searchList == '') {
      //   this.list = data.list
      //   console.log('没有搜索内容')
      // } else {
      //   this.list = fuse.search(this.queryForm.searchList)
      //   console.log('有搜索内容走这里')
      // }
      // console.log('this.list', this.list)
      this.total = data.totalCount
      console.log('this.total', this.total)
      this.timeOutID = setTimeout(() => {
        this.listLoading = false
      }, 300)
    },
  },
}
</script>
